﻿cmake_minimum_required (VERSION 3.8)

project(LibDGGSKernel CXX)

# 设置编译标准
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

if (CMAKE_BUILD_TYPE STREQUAL "Debug")
    set(DGGS_OUT_DIR ${CMAKE_OUT_DIR}/out/build/debug)
else ()
    set(DGGS_OUT_DIR ${CMAKE_OUT_DIR}/out/build/release)
endif ()


set(lib_install_directory ${CMAKE_SOURCE_DIR}/out)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${DGGS_OUT_DIR}/LibDGGSKernel)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${DGGS_OUT_DIR}/LibDGGSKernel)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${DGGS_OUT_DIR}/LibDGGSKernel)

set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)

set(CMAKE_DEBUG_POSTFIX D)
set(CMAKE_RELEASE_POSTFIX )

include_directories(${CMAKE_SOURCE_DIR}/3rdlibs)
include_directories(${CMAKE_SOURCE_DIR}/LibDGGSBase)

# 获取所有 .cpp 文件
file(GLOB_RECURSE SOURCE_FILES "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp")

add_library(LibDGGSKernel SHARED ${SOURCE_FILES})

target_compile_definitions(LibDGGSKernel PRIVATE BUILDING_DLL)


if(ISEXTRACT)
    # LibDGGSBase复制
    file(GLOB_RECURSE MyLIB_HEADERS ${CMAKE_SOURCE_DIR}/LibDGGSBase/*.hpp)
    foreach(header_file ${MyLIB_HEADERS})
        get_filename_component(header_dir ${header_file} DIRECTORY)
        string(REPLACE ${CMAKE_SOURCE_DIR}/LibDGGSBase ${lib_output_directory}/include/LibDGGSBase output_header_dir ${header_dir})
        file(MAKE_DIRECTORY ${output_header_dir})
        configure_file(${header_file} ${output_header_dir} COPYONLY)
    endforeach()

    #LibDGGSKernel复制
    set(header_file ${CMAKE_SOURCE_DIR}/LibDGGSKernel/IDGGSKernel.hpp)
    get_filename_component(header_dir ${header_file} DIRECTORY)
    string(REPLACE ${CMAKE_SOURCE_DIR}/LibDGGSKernel ${lib_output_directory}/include/LibDGGSKernel output_header_dir ${header_dir})
    file(MAKE_DIRECTORY ${output_header_dir})
    configure_file(${header_file} ${output_header_dir} COPYONLY)

    if (CMAKE_BUILD_TYPE STREQUAL "Debug")
        add_custom_command(TARGET LibDGGSKernel POST_BUILD

                COMMAND ${CMAKE_COMMAND} -E copy
                ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/LibDGGSKernelD.lib
                ${lib_output_directory}/lib/debug/LibDGGSKernelD.lib


                COMMAND ${CMAKE_COMMAND} -E copy
                ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}/LibDGGSKernelD.dll
                ${lib_output_directory}/bin/debug/LibDGGSKernelD.dll
                COMMENT "Copying LibDGGSKernel debug files..."
        )
    else ()
        add_custom_command(TARGET LibDGGSKernel POST_BUILD

                COMMAND ${CMAKE_COMMAND} -E copy
                ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/LibDGGSKernel.lib
                ${lib_output_directory}/lib/release/LibDGGSKernel.lib

                COMMAND ${CMAKE_COMMAND} -E copy
                ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}/LibDGGSKernel.dll
                ${lib_output_directory}/bin/release/LibDGGSKernel.dll
                COMMENT "Copying LibDGGSKernel release files..."
        )
    endif ()
endif()


install(TARGETS LibDGGSKernel
        LIBRARY DESTINATION ${lib_install_directory}/lib
        ARCHIVE DESTINATION ${lib_install_directory}/lib
        RUNTIME DESTINATION ${lib_install_directory}/bin
        PUBLIC_HEADER DESTINATION ${lib_install_directory}/include
)
install(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/" # source directory
        DESTINATION ${lib_install_directory}/include # target directory
        FILES_MATCHING # install only matched files
        PATTERN "*.hpp" # select header files
)
